<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * @version $Id: show_player.tpl.php 1151 2008-06-24 15:07:32Z julienv $
 * @package Joomleague
 * @copyright Copyright (C) 2007  JoomLeague
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see _joomleague_license.txt
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 
  
  // TODO use a generic function !
  /**
   * gives games played/came in/came out stats for player
   *
   * @param int $player_id
   * @param int $project_id
   * @param int $team_id
   * @param int $position_id (if null, all positions)
   * @return Object (members: played, subst, sub_out)
   */
  function getStartInOutStats($player_id, $project_id, $team_id, $position_id = 0) 
  {
    global $database;
    $query = "SELECT count(mp.id) as played, sum(mp.came_in) as subst, sum(mp.out = 1) AS sub_out
              FROM #__joomleague_match_players AS mp 
              INNER JOIN #__joomleague_matches AS m ON mp.match_id = m.match_id 
              WHERE mp.player_id = '$player_id' 
                AND m.project_id = '$project_id'
                AND mp.team_id = '$team_id'";
    if ($position_id) {
      $query .= ' AND mp.position_id = ' . $position_id;
    }
    $database->setQuery($query);
    $result = NULL;
    $database->loadObject($result);
    return $result;
  }
  
?>
<div id="joomleague">
<!-- Header with Project Name-->
<?php if ($showPlayer_config['show_project_heading'] == "1" OR $overall_config['show_project_heading'] == "1") { ?>
<div class="componentheading" <?php if ($showPlayer_config['show_print_button'] == "1" OR $overall_config['show_print_button'] == "1") echo 'style="float:left;"';?>><?php echo $htmlHelper->showTitle($joomleague, $overall_config['project_heading_type']); ?></div>
<!-- Druckfunktion -->
<?php if ($showPlayer_config['show_print_button'] == "1" OR $overall_config['show_print_button'] == "1") {
$htmlHelper->PrintIcon( $row, $params, false, '' );
};?>
<!-- Druckfunktion Ende -->
<?php } ?>
<!-- Header END -->

<!-- Bereichsüberschrift z.B Tabelle, Tippabgabe usw. bei Bedarf mit Drucksymbol -->
<table width="100%" class="contentpaneopen">
	<tr>
		<td class="contentheading"><?php echo sprintf ('%s %s' ,$row->firstname,$row->lastname);?>
    	<?php if($row->injury > 0 && $row->injury_end > $joomleague->current_round){
        echo '<img src="'.JL_TEMPLATE_FOLDERS_URL.'/images/injured.gif" alt="" />';
              }?>
    	<?php if($row->suspension > 0 && $row->suspension_end > $joomleague->current_round){
        echo '<img src="'.JL_TEMPLATE_FOLDERS_URL.'/images/suspension.gif" alt="" />';
              }?>              
    </td>
	</tr>
</table>
<!-- Bereichsüberschrift Ende -->
<br />
  
<table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
	<tr class="sectiontableheader">
	 <td colspan="2"><?php echo _JL_PLAYER_DATA ;?></td>
	</tr>
<tr><td>&nbsp;</td></tr>
	<tr>
		<?php 
		if ($showPlayer_config['show_player_photo']==1) 
		{
			// look for picture in Playertool first
		  if ( $row->picture && file_exists( _JOOMLA_JL_PATH.'/'.$row->picture ) ) {
		  	 $picture = $row->picture;
		  }
		  // otherzise, look for defqult picture on player objet
		  else if ( $row->default_picture && file_exists( _JOOMLA_JL_PATH.'/'.$row->default_picture ) ) {
		  	$picture = $row->default_picture;
		  }
		  else {
		  	$picture = null;
		  }
		  
		  if ( $picture != null ) 
		  {
		    ?>
		    <td width="50%" align="center" valign="middle">
		      <?php 
		      echo sprintf("<img src=\""._JOOMLA_JL_LIVE_SITE."/%s\" alt=\"%s\" title=\"%s\" />",
		                    $picture,
		                    $row->lastname,
		                    $row->lastname);
		      ?>&nbsp;
		    </td>
		    <?php 
		  }
		}?>      
      <td width="50%" align="center" valign="top">
        <table width="100%" border="0">
          <tr>
            <td width="40%"><b><?php echo _JL_PLAYER_NAME;?></b></td>
            <td width="60%" align="left"><?php 
            $name = sprintf("%s %s",$row->firstname,$row->lastname);
            switch ( $showPlayer_config['show_user_profile'] ) {
            	case 1:
            		if ($contact_id) echo '<a href="'.sefRelToAbs("index.php?option=com_contact&task=view&contact_id=$contact_id").'">'.$name.'</a>';
            		else echo $name;
            		break;
            	case 2: 
            	  echo '<a href="'.sefRelToAbs("index.php?option=com_comprofiler&task=userProfile&user=$row->jl_user_id").'">'.$name.'</a>';
            		break;
            	default:
            		echo $name;
            		break;
            }
            ?>
            <?php
      $thiscountry = new Country($database);
      $thiscountry->load($row->nation);
      echo '&nbsp;&nbsp;'.$thiscountry->getFlag();
     ?>
            
            </td>
          </tr>
<?php if (isset($showPlayer_config['show_birthday']) && $showPlayer_config['show_birthday'] == 1 && $row->birthday != '0000-00-00') { ?>
          <tr>
            <td><b><?php echo _JL_PLAYER_BIRTHDAY;?></b></td>
            <td align="left">
              <?php 
              printf( "%s (%s)", 
                      mbiFormatDate( $row->birthday, _JL_DAYDATE ), 
                      getAge( $row->birthday, $joomleague->start_date )); 
              ?>
            </td>
          </tr>
<?php } ?>
<?php if($row->position != ""){ ?>
          <tr>
            <td><b><?php echo _JL_PLAYER_POSITION;?></b></td>
            <td align="left"><?php echo $row->position;?></td>
          </tr>
<?php } ?>
<?php if (($showPlayer_config['show_player_number']) && ($row->position_number > 0)) { ?>
          <tr>
            <td><b><?php echo _JL_PLAYER_NUMBER;?></b></td>
            <td align="left">
        		<?php 
        		if ($showPlayer_config['player_number_picture']) { ?>
          		<img src="<?php echo JL_TEMPLATE_FOLDERS_URL;?>/images/shirt.php?text=<?php echo $row->position_number; ?>" 
          			alt="<?php echo $row->position_number;?>" title="<?php echo $row->position_number;?>" /><?php
            }
            else {
              echo $row->position_number;
            } 
            ?>            
            </td>
          </tr>
<?php } ?>
<?php if($row->height > 0){ ?>
          <tr>
            <td><b><?php echo _JL_PLAYER_HEIGHT;?></b></td>
            <td align="left"><?php printf("%s cm",$row->height); ?></td>
          </tr>
<?php } ?>
<?php if($row->weight > 0){ ?>
          <tr>
            <td><b><?php echo _JL_PLAYER_WEIGHT;?></b></td>
            <td align="left"><?php printf("%s kg",$row->weight);?></td>
          </tr>
<tr><td>&nbsp;</td></tr>          
<?php } ?>
<?php if($row->injury > 0 && $row->injury_end > $joomleague->current_round){ ?>
          <tr>
            <td style="color: red"><b><?php echo _JL_PLAYER_INJURY_END;?></b></td>
            <td align="left"><?php printf("%s. %s",$row->injury_end, _JL_MATCHDAY); ?></td>
          </tr>
          <tr>
            <td nowrap="nowrap"><b><?php echo _JL_PLAYER_INJURY_TYPE;?></b></td>
            <td align="left"><?php printf("%s", htmlspecialchars($row->injury_detail)); ?></td>
          </tr>          
<?php } ?>
<?php if($row->suspension > 0 && $row->suspension_end > $joomleague->current_round){ ?>
          <tr>
            <td style="color: red"><b><?php echo _JL_PLAYER_SUSPENSION_END;?></b></td>
            <td align="left"><?php printf("%s. %s",$row->suspension_end, _JL_MATCHDAY); ?></td>
          </tr>
          <tr>
            <td nowrap="nowrap"><b><?php echo _JL_PLAYER_SUSPENSION_REASON;?></b></td>
            <td align="left"><?php printf("%s",htmlspecialchars($row->suspension_detail)); ?></td>
          </tr>
<?php } ?>
        </table>
      </td>
    </tr>

    </table>
  <br /><br />
  
<!-- Player description START --> 
<?php 
if ( ( $showPlayer_config['show_player_general_description'] == 1 && $row->pdescription != '' )
  || ( $showPlayer_config['show_player_description'] == 1 && $row->ptdescription != '' ) ) 
{
  ?>
  <table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
  	<tr class="sectiontableheader">
  		<td><?php echo _JL_ADMIN_EDIT_FORM_PLAYERTOOL_TITLE2;?></td>
  	</tr>
  <tr><td>&nbsp;</td></tr>
  <?php
  if ( $showPlayer_config['show_player_general_description'] == 1 && $row->pdescription != '' )
  {
  	?>
    <tr>
  		<td>
  			<?php 
        echo $htmlHelper->printContentMambot( $row->pdescription );
        ?>
  		</td>
  	</tr>
    <?php 
  }
  if ( $showPlayer_config['show_player_description'] == 1 && $row->ptdescription != '' )
  {	      
    ?>	
  	<tr>
  		<td>
  			<?php 
        echo $htmlHelper->printContentMambot( $row->ptdescription );
        ?>
  		</td>
  	</tr>
    <?php 
  }
  ?>
  </table>
  <br /><br />
  <?php 
}
?>
<!-- Player description END --> 

<!-- Player history START -->  
  <?php 
  if ($showPlayer_config['show_player_history'] && count($history) > 0) { 
  ?>
  <table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
		<tr class="sectiontableheader">
			<td><?php echo _JL_PLAYER_HISTORY;?></td>
		</tr>
	  <tr>
		  <td>
			  <table id="player_history" width="100%" cellspacing="2" cellpadding="2" border="0">
			  <tr class="sectiontableheader">
			    <th><?php echo _JL_PLAYER_PROJECT;?></th>
			    <th><?php echo _JL_PLAYER_SEASON;?></th>
			    <th><?php echo _JL_TABLE_TEAM;?></th>
			    <th><?php echo _JL_PLAYER_POSITION;?></th>
			  </tr>
				<?php
				foreach ($history AS $station) {	?>
		    <tr><?php 
		        echo   '<td>'
                 . '<a href="'.sefRelToAbs(JL_COMPONENT_LINK.'&amp;func=showPlayer&amp;p='.$station->project_id.'&amp;pid='.$pid.JL_ITEMID_SUFFIX).'">'
                 . $station->pname
                 . '</a>'
                 . '</td>'
                 . '<td>'.$station->sname.'</td>'
		             . '<td>'
		             . '<a href="'.sefRelToAbs(JL_COMPONENT_LINK.'&amp;func=showPlayers&amp;p='.$station->project_id.'&amp;ttid='.$station->ttid.JL_ITEMID_SUFFIX).'">'
		             . $station->teamname
		             . '</a>'
		             . '</td>'
		             . '<td>'.$station->position.'</td>'; ?>
      	</tr><?php 
        } ?>
	      </table>
	    </td>
	  </tr>
  </table> 
<br /> 
<?php } ?>
<!-- Player history END --> 

<!-- Staff history START --> 
<?php if ($showPlayer_config['show_player_history'] && count($historyStaff) > 0) {;?>
<table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
	<tr class="sectiontableheader">
		<td><?php echo _JL_STAFF_HISTORY;?></td>
	</tr>
  <tr>
		<td>
		  <table id="staff_history" width="100%" cellspacing="2" cellpadding="2" border="0">
		   <tr class="sectiontableheader">
          <th><?php echo _JL_PLAYER_PROJECT;?></th>
          <th><?php echo _JL_PLAYER_SEASON;?></th>
          <th><?php echo _JL_TABLE_TEAM;?></th>
		      <th><?php echo _JL_PLAYERS_FUNCTION;?></th>
		  </tr><?php
      foreach ($historyStaff AS $station) {	?>
			<tr><?php
        echo  '<td>'
            . '<a href="'.sefRelToAbs(JL_COMPONENT_LINK.'&amp;func=showPlayer&amp;p='.$station->project_id.'&amp;pid='.$pid.JL_ITEMID_SUFFIX).'">'
            . $station->pname
            . '</a>'
            . '</td>'
            . '<td>'.$station->sname.'</td>'
            . '<td>'
            . '<a href="'.sefRelToAbs(JL_COMPONENT_LINK.'&amp;func=showPlayers&amp;p='.$station->project_id.'&amp;ttid='.$station->ttid.JL_ITEMID_SUFFIX).'">'
            . $station->teamname
            . '</a>'
            . '</td>'
            . '<td>'.$station->position.'</td>';?>
	   </tr><?php 
     } ?>
	   </table>
	 </td>
 </tr>
</table>
<br /><br />
<?php };?>
<!-- Staff history END --> 

<!-- Player stats START --> 
<?php if ($showPlayer_config['show_player_statistic']==1) {;?>
<table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">

	<tr class="sectiontableheader">
		<td colspan="3"><?php echo _JL_PLAYER_STATS;?></td>
	</tr>
  <?php
  if ( $overall_config['use_jl_substitution'] ) {
    $query = "SELECT count(mp.id) as played, sum(mp.came_in) as subst, sum(mp.out = 1) AS sub_out
               FROM #__joomleague_match_players AS mp LEFT join #__joomleague_matches AS m ON mp.match_id = m.match_id 
               WHERE mp.player_id = '".$row->player_id."' AND m.project_id = '".$joomleague->id."'";
    $database->setQuery($query);
    $inoutstat = $database->loadObjectList();
    
    // first, games played
    if ($showPlayer_config['show_games_played']) { ?>
      <tr>
        <td width="20" align="center" class="<?php echo $class;?>">
        	<img src="<?php echo JL_TEMPLATE_FOLDERS_URL.'/images/shirt.png';?>" width="20" alt="<?php echo _JL_PLAYER_PLAYED;?>" />
  			</td>
        <td nowrap="nowrap"><b><?php echo _JL_PLAYER_PLAYED;?></b></td>
        <td align="left"><?php echo ($inoutstat[0]->played > 0) ? $inoutstat[0]->played : '-'; ?></td>
      </tr><?php
    }
    if ($showPlayer_config['show_substitution_stats']) {?>
      <tr>
     		<td width="20" align="center" class="<?php echo $class;?>"></td>
        <td nowrap="nowrap"><b><?php echo _JL_STARTING_ROSTERS;?>:</b></td>
        <td align="left"><?php
              echo ($inoutstat[0]->played - $inoutstat[0]->subst > 0) ? $inoutstat[0]->played - $inoutstat[0]->subst : '-';?>
        </td>
      </tr>
      <tr>
        <td width="20" align="center" class="<?php echo $class;?>">
        	<img src="<?php echo JL_TEMPLATE_FOLDERS_URL.'/images/in.png';?>" alt="<?php echo _JL_PLAYER_IN;?>" />
        </td>
        <td nowrap="nowrap"><b><?php echo _JL_PLAYER_IN;?>:</b></td>
        <td align="left"><?php echo ($inoutstat[0]->subst >0) ? $inoutstat[0]->subst : '-'; ?></td>
    	</tr>
      <tr>
        <td width="20" align="center" class="<?php echo $class;?>">
    	    <img src="<?php echo JL_TEMPLATE_FOLDERS_URL.'/images/out.png';?>" alt="<?php echo _JL_PLAYER_OUT;?>" />
        </td>
        <td nowrap="nowrap"><b><?php echo _JL_PLAYER_OUT;?>:</b></td>
        <td align="left"><?php echo ($inoutstat[0]->sub_out >0) ? $inoutstat[0]->sub_out : '-'; ?></td>
  		</tr>
      <?php 
    }
  }

  if ( $overall_config['use_jl_events'] ) {
    $k = 1;
    if (count($eventtypes)>0) {
      foreach($eventtypes as $eventtype){
    	$k = 1 - $k;
        $class = ($k == 1)? 'sectiontableentry1' : 'sectiontableentry2';
    	$scores=$joomleague->getPlayerEvents($row->player_id, $eventtype->eventtype_id);
      ?>	
  		<tr>
  			<td width="20" align="center" class="<?php echo $class;?>">
  				<img src="<?php echo _JOOMLA_JL_LIVE_SITE; ?>/images/joomleague/event_icons/<?php echo $eventtype->icon;?>" alt="<?php echo $eventtype->name;?>" title="<?php echo $eventtype->name;?>" />
  			</td>
  			<td class="<?php echo $class;?>">
  				<b><?php echo $eventtype->name;?>:</b>
  			</td>
        <td class="<?php echo $class;?>">
        	<b><?php echo ($scores>0)?$scores:'-';?></b>
        </td>
      </tr>
      <?php
   	  }
   	}
  }
  ?>
	</table>
    <br /><br />
<?php };?>
<!-- Player stats END --> 

<!-- Player stats History START --> 
<?php 
if ($showPlayer_config['show_player_statistic_history']==1) 
{
  ?>
	<table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
	  <tr class="sectiontableheader">
	    <td><?php echo _JL_PLAYER_STATS;?></td>
	  </tr>
	  <tr>
	    <td>
	    <table id="stats_history" width="100%" cellspacing="2" cellpadding="2" border="0">
	      <!-- header start -->
	      <thead>
	      <tr class="sectiontableheader">
	          <th><?php echo _JL_PLAYER_PROJECT;?></th>
	          <th><?php echo _JL_TABLE_TEAM;?></th>
              <?php
                if ($showPlayer_config['show_games_played']) { ?>
	          <th><?php echo _JL_PLAYER_PLAYED;?></th>
	          <?php 
	            }
                if ($showPlayer_config['show_substitution_stats']) {

	            if (isset($overall_config['use_jl_substitution']) && $overall_config['use_jl_substitution'] == 1) 
	            {
	             ?>
		            <th><?php echo _JL_STARTING_ROSTERS;?></th>
		            <th><img src="<?php echo JL_TEMPLATE_FOLDERS_URL.'/images/in.png';?>" alt="<?php echo _JL_PLAYER_IN;?>" />
		            </th>
		            <th><img src="<?php echo JL_TEMPLATE_FOLDERS_URL.'/images/out.png';?>" alt="<?php echo _JL_PLAYER_OUT;?>" />
		            </th>
		            <?php 
	            }
	            }
                if ( $overall_config['use_jl_events'] ) {
	              if (count($eventtypes)>0) 
	              {
	                foreach($eventtypes as $eventtype) 
	                {
	                ?>
  	              <th><?php echo $eventtype->name;?></th><?php
	                }
	              }
	            }
	            ?>
	      </tr>
	      </thead>
	      <!-- header end -->  
        <?php
		    $career= array();
		    foreach ($rows as $player_hist) 
		    {		      
          $proj = new Joomleague($database);
          $proj->id = $player_hist->project_id;
          
		    	if (!isset($career['played'])) $career['played'] = 0;
		      
		      $inoutstat = getStartInOutStats($pid, $player_hist->project_id, $player_hist->team_id);
		      $career['played'] += $inoutstat->played;
		      ?>
		      <tr>
        <td align="left"><?php echo $player_hist->proj_name; ?></td>
        <td align="left"><?php echo $player_hist->team_name; ?></td>
        <?php if ($showPlayer_config['show_games_played']) { ?>
        <td align="left"><?php echo ($inoutstat->played > 0) ? $inoutstat->played : '-'; ?></td>
        <?php
        }
        //substitution system 
        if ($showPlayer_config['show_substitution_stats']) {
        if (isset($overall_config['use_jl_substitution']) && $overall_config['use_jl_substitution'] == 1) 
        {
          if (!isset($career['started'])) $career['started'] = 0;
          if (!isset($career['in'])) $career['in'] = 0;
          if (!isset($career['out'])) $career['out'] = 0;
          
          $career['started']+=$inoutstat->played - $inoutstat->subst;
          $career['in']+=$inoutstat->subst;
          $career['out']+=$inoutstat->sub_out; ?>
          <td align="left"><?php
            echo ($inoutstat->played - $inoutstat->subst > 0) ? $inoutstat->played - $inoutstat->subst : '-';?>
          </td>
          <td align="left"><?php echo ($inoutstat->subst > 0) ? $inoutstat->subst : '-'; ?></td>
          <td align="left"><?php echo ($inoutstat->sub_out > 0) ? $inoutstat->sub_out : '-'; ?></td><?php 
        }
        }
        // stats per project
          if ( $overall_config['use_jl_events'] ) if (count($eventtypes)>0) 
        {
          foreach($eventtypes as $eventtype) 
          {
            if ($proj->id) 
            {
              $stat=$proj->getPlayerEvents($pid, $eventtype->eventtype_id, $player_hist->team_id);
              
              if (!isset($career[$eventtype->eventtype_id])) {
                $career[$eventtype->eventtype_id] = 0;
              }
              $career[$eventtype->eventtype_id]+=$stat;
              
            }
            else $stat = "-";
            ?>
            <td><?php echo ($stat>0)?$stat:'-';?></td><?php 
          }
        } 
        ?>
      </tr><?php
    } ?>
      <tr class="career_stats_total">
        <td align="left"><?php echo "Career"; ?></td>
        <td align="left">&nbsp;</td>
        <?php if ($showPlayer_config['show_games_played']) { ?>
        <td align="left"><?php echo $career['played']; ?></td>
        <?php
        }
        //substitution system 
        if ($showPlayer_config['show_substitution_stats']) {
        if (isset($overall_config['use_jl_substitution']) && $overall_config['use_jl_substitution'] == 1) {?>
         <td align="left"><?php echo $career['started'];?></td>
         <td align="left"><?php echo $career['in']; ?></td>
         <td align="left"><?php echo $career['out']; ?></td><?php
        }
        }
        // stats per project
          if ( $overall_config['use_jl_events'] ) if (count($eventtypes)>0) {
          foreach($eventtypes as $eventtype) { 
            if ((!isset($overall_config['event_goalie_gagavg'])) || (!($eventtype->eventtype_id == $overall_config['event_goalie_gagavg']))) {?>
              <td><?php echo $career[$eventtype->eventtype_id];?></td><?php
            }
            else {
              if ($career['played']) {
              ?>
              <td><?php printf('%.2f', $career[$overall_config['event_goals_allowed']]/$career['played']);?></td><?php
              }
            }
          }
        } ?>
      </tr>
    </table>
    </td>
  </tr>
</table>
<?php };?>
<!-- Player stats END --> 

<!-- Print Button START -->
<?php if ($showPlayer_config['show_back_button'] > 0 OR $overall_config['show_back_button'] > 0) {;?>
<?php if ($showPlayer_config['show_back_button'] == "1" OR $overall_config['show_back_button'] == "1") echo '<div align="left">'; else echo '<div align="right">';?>
<?php
$params = & new mosParameters( '' );
$params->def( 'back_button', true );
$params->def( 'popup', false );
mosHTML::BackButton( $params );
  echo "</div>";
};?>
<!-- Print Button END -->  
</div>